package com.itheima.dbservice;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.dto.UserInfoDto;
import com.itheima.exception.CommonException;
import com.itheima.pojo.UserInfo;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserInfoService {
    @DubboReference(timeout = 1000)
    private UserInfoServiceApi userInfoServiceApi;
    @Autowired
    private SsoService ssoService;

    public void loginReginfo(UserInfoDto userInfoDto, String token) {
        //解析token并输出日志
        log.info("loginReginfo:" + token);
        Claims claimsBody = JwtUtils.getClaimsBody(token);
        int i = JwtUtils.verifyToken(claimsBody);
        if (i == 1 || i == 2){
            throw new CommonException("用户未登录");
        }

        Long id = Convert.toLong(claimsBody.get("id"));
        log.info("当前登录用户id为" + id);

        UserInfo userInfo = new UserInfo();
        BeanUtil.copyProperties(userInfoDto, userInfo);
        userInfo.setId(id);

        userInfoServiceApi.saveUserInfo(userInfo);
    }

    //更新头像
    public void updateImage(UserInfo userInfo) {
        userInfoServiceApi.updateImage(userInfo);
    }
}
